//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//
//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2003 Intrinsyc Europe Ltd.
//////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2004 Siemens AG
//
// Version: 1.1.2.5
// Date:    11. Jan. 2006
//////////////////////////////////////////////////////////////////////////////


// Activate binary trace of data passed to application
//#define SIMTK_ENABLE_BIN_TRACE

// Proactive SAT commands
#define SIMTK_CMD_REFRESH                1
#define SIMTK_CMD_SETUP_EVENTLIST        5
#define SIMTK_CMD_SETUP_CALL             16
#define SIMTK_CMD_SEND_SS                17
#define SIMTK_CMD_SEND_USSD              18
#define SIMTK_CMD_SEND_SMS               19
#define SIMTK_CMD_SEND_DTMF              20
#define SIMTK_CMD_LAUNCH_BROWSER         21
#define SIMTK_CMD_PLAY_TONE              32
#define SIMTK_CMD_DISPLAY_TEXT           33
#define SIMTK_CMD_GET_INKEY              34
#define SIMTK_CMD_GET_INPUT              35
#define SIMTK_CMD_SELECT_ITEM            36
#define SIMTK_CMD_SETUP_MENU             37
#define SIMTK_CMD_SETUP_IDLE_TEXT        40
#define SIMTK_CMD_GET_ICON_DATA          250

// Envelope SAT commands
#define SIMTK_EVT_MENU_SELECTION         211
#define SIMTK_EVT_USER_ACTIVITY          232
#define SIMTK_EVT_IDLESCREEN_AVAILABLE   233
#define SIMTK_EVT_LANGUAGE_SELECTION     235
#define SIMTK_EVT_BROWSER_TERMINATION    236
#define SIMTK_EVT_END_SESSION            254

// Special Siemens SAT response
#define SIMTK_RSP_SCREEN_BUSY            132

// Maximal length of SAT command
#define SIMTK_MAX_LENGTH                 0x2000
#define UCS2_LEN                         (MAX_ATCMD_LEN / 2)

// SAT command retries
#define SIMTK_CMD_RETRIES                1
#define SIMTK_CMD_RETRY_DELAY            2000

// Default header for several SAT command text fields
#define SIMTK_TXT_STANDARD               "0D0008"
#define SIMTK_TXT_DEFAULT                "170008"
#define SIMTK_TXT_ALPHA                  "050080"
#define SIMTK_TXT_ITEM                   "8F000080"

#define SIMTK_TXT_STANDARD_SIZE          (sizeof(SIMTK_TXT_STANDARD) - 1)
#define SIMTK_TXT_DEFAULT_SIZE           (sizeof(SIMTK_TXT_DEFAULT) - 1)
#define SIMTK_TXT_ALPHA_SIZE             (sizeof(SIMTK_TXT_ALPHA) - 1)
#define SIMTK_TXT_ITEM_SIZE              (sizeof(SIMTK_TXT_ITEM) - 1)
#define SIMTK_TXT_MAX_SIZE               (2 * 254)

// Some support macros
#define IS_EVEN(x)   (((x >> 1) << 1) == x)
#define ADD_DIGIT(p, i, x)   { if (IS_EVEN(i)) { p[i] = 'F'; p[i + 1] = x; p[i + 2] = '\0'; } else { p[i - 1] = x; } i++; }
#define SIMTK_PREPARE_TEXT(szTxt, pTxt, szInit)   strcpy(szTxt, szInit); pTxt = szTxt + (sizeof(szInit) - 1);

#ifdef RIL_ENABLE_SIMTK
#define SAT_INLINE_VOID
#define SAT_INLINE_HRESULT
#define SAT_INLINE_BOOL
#else // RIL_ENABLE_SIMTK
#define SAT_INLINE_VOID                  { }
#define SAT_INLINE_HRESULT               { return E_NOTIMPL; }
#define SAT_INLINE_BOOL                  { return TRUE; }
#endif // RIL_ENABLE_SIMTK

#ifdef SIEMENS_MAGNETO
#define SAT_PARSE_PARAM                  LPCSTR szRspIn, void*& pBlob, UINT& cbBlob
#else // SIEMENS_MAGNETO
#define SAT_PARSE_PARAM                  LPCSTR szRspIn, void*& pBlob, UINT& cbBlob, BOOL fUseGSM = FALSE
#endif // SIEMENS_MAGNETO

//
// Class implementation for SIM-Toolkit handling
//
class CSimToolkitHandling {
public:
  CSimToolkitHandling() SAT_INLINE_VOID;
  ~CSimToolkitHandling() SAT_INLINE_VOID;
  BOOL              Init(CRilHandle* pRilDevice) SAT_INLINE_BOOL;
  BOOL              Deinit(void) SAT_INLINE_BOOL;
  HRESULT           GetProfile(CRilInstanceHandle* pHandle) SAT_INLINE_HRESULT;
  HRESULT           SetProfile(CRilInstanceHandle* pHandle) SAT_INLINE_HRESULT;
  BOOL              ParseNotification(LPCSTR& rszPointer) SAT_INLINE_BOOL;
  static HRESULT    ParseGetDevCapsNotifications(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam) SAT_INLINE_HRESULT;

#ifdef SIEMENS_MAGNETO

  HRESULT           SendCmdResponse(CRilInstanceHandle* pHandle, const RILSIMTOOLKITRSP* pRsp, LPBYTE pDetails, const DWORD dwDetailSize) SAT_INLINE_HRESULT;
  HRESULT           SendEventDownload(CRilInstanceHandle* pHandle, const DWORD dwEvent, LPBYTE pData, const DWORD dwDataSize) SAT_INLINE_HRESULT;

#else // SIEMENS_MAGNETO

  HRESULT           SendCmdResponse(CRilInstanceHandle* pHandle, const BYTE* lpbResponse, DWORD dwSize) SAT_INLINE_HRESULT;
  HRESULT           SendEnvelopeCmd(CRilInstanceHandle* pHandle, const BYTE* lpbCommand, DWORD dwSize) SAT_INLINE_HRESULT;

#endif // SIEMENS_MAGNETO

#ifdef RIL_ENABLE_SIMTK

private:
  HRESULT           QueueSimToolkitCmd(DWORD dwCmd);
  static HRESULT    ParseGetCommand(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  static HRESULT    ParseRspError(LPCSTR szRsp, LPVOID pParam);
  static HRESULT    ParseGetProfile(LPCSTR szRsp, void*& pBlob, UINT& cbBlob, LPVOID pParam);
  BOOL              ParseRefresh(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  BOOL              ParseSetupCall(SAT_PARSE_PARAM);
  BOOL              ParseSendSS(SAT_PARSE_PARAM);
  BOOL              ParseSendUSSD(SAT_PARSE_PARAM);
  BOOL              ParseSendSMS(SAT_PARSE_PARAM);
  BOOL              ParsePlayTone(SAT_PARSE_PARAM);
  BOOL              ParseDisplayText(SAT_PARSE_PARAM);
  BOOL              ParseGetInkey(SAT_PARSE_PARAM);
  BOOL              ParseGetInput(SAT_PARSE_PARAM);
  BOOL              ParseSelectItem(SAT_PARSE_PARAM);
  BOOL              ParseSetupMenu(SAT_PARSE_PARAM);
  BOOL              ParseIdleText(SAT_PARSE_PARAM);
  void              TraceBlob(BYTE* pBlob, DWORD dwSize);

#ifdef SIEMENS_MAGNETO

  BOOL              HandleBlob(DWORD dwNotificationCode, void*& pBlob, UINT& cbBlob, BYTE* pData, DWORD dwSize);
  BOOL              PrepareRilCmdStruct(BYTE* pBuffer, RILSIMTOOLKITCMD** pRilCmd, DWORD dwType);
  BOOL              PrepareRilCmdError(RILSIMTOOLKITCMD* pRilCmd, DWORD dwError);
  BOOL              ParseSetupEventList(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  BOOL              ParseSendDTMF(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);
  BOOL              ParseLaunchBrowser(LPCSTR szRsp, void*& pBlob, UINT& cbBlob);

  BYTE              m_bCmdBuffer[SIMTK_MAX_LENGTH];

#else // SIEMENS_MAGNETO

  BOOL              HandleBlob(DWORD dwNotificationCode, void*& pBlob, UINT& cbBlob, char* szCmd);
  void              ConvertSATBlob(char* szSATCmd, BYTE* pBlob);
  void              ConvertSATString(char* lpszSATString);
  void              ConvertSATAddress(char* lpszSATString);
  BOOL              UpdateTextField(char* szSATText, BOOL fUseGSM);
  BOOL              UpdateAlphaField(char* szSATText, BOOL fUseGSM);
  BOOL              UpdateItemField(char* szSATText, UINT nItem, BOOL fUseGSM);
  BOOL              AddSATCmdLength(char* szSATCmd);

  char              m_szCmdBuffer[SIMTK_MAX_LENGTH];

#endif // SIEMENS_MAGNETO

  CRITICAL_SECTION  m_csLockBuffer;
  CRilHandle*       m_pRilDevice;
  BOOL              m_fNoResponse;

#endif // RIL_ENABLE_SIMTK
};

